package io.bdmc.core.snmp;

import io.bdmc.common.utils.StringUtils;
import io.bdmc.modules.bss.entity.DeviceAlarmType;
import io.bdmc.modules.bss.entity.DeviceMeasure;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

@Component
public class SnmpDataCache {

    // 报警类型缓存列表,只在系统启动的时候初始化 后面只读
    private static List<DeviceAlarmType> alarmTypeList = new ArrayList<>();

    // trap消息队列
    private static final ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();

    // property oid 对称表 只在系统启动的时候初始化,后面只读。
    private static final ConcurrentHashMap<String, String> devicePropertyOidHashMap = new ConcurrentHashMap<>(128);

    // 短信发送的数据
    private static ConcurrentLinkedQueue<SnmpSMSData> smsQueue = new ConcurrentLinkedQueue<>();

    private static ConcurrentLinkedQueue<DeviceMeasure> opmMeasureQueue = new ConcurrentLinkedQueue<>();

    /**
     * 通过报警代码获取报警类型的详细信息
     * 
     * @param code  告警代码
     * @param level 告警等级，可不传
     * @return DeviceAlarmType对象
     */
    public static DeviceAlarmType getAlarmTypeListByCode(String code, String level) {
        Optional<DeviceAlarmType> dType;
        if (!StringUtils.isEmpty(level)) {
            dType = alarmTypeList.stream()
                    .filter(al -> al.getAlarmTypeCode().equals(code) && al.getAlarmLevelCode().contains(level))
                    .findFirst();
        } else {
            dType = alarmTypeList.stream().filter(al -> al.getAlarmTypeCode().equals(code)).findFirst();
        }
        return dType.orElse(null);
    }

    public static DeviceAlarmType getAlarmTypeListByCode(String code) {
        Optional<DeviceAlarmType> dType = alarmTypeList.stream().filter(al -> al.getAlarmTypeCode().equals(code)).findFirst();
        return dType.orElse(null);
    }

    public static void setAlarmTypeList(List<DeviceAlarmType> list) {
        alarmTypeList = list;
    }

    /**
     * 将oid--属性对应的键值对放到缓存
     */
    public static void putOid2PropertyToMap(String oid, String property) {
        devicePropertyOidHashMap.put(oid, property);
    }

    /**
     * 将属性--oid对应的键值对放到缓存
     */
    public static void putProperty2oidToMap(String property, String oid) {
        devicePropertyOidHashMap.put(property, oid);
    }

    /**
     * 通过oid获取属性名称
     */
    public static String getPropertyByOid(String oid) {
        return devicePropertyOidHashMap.get(oid);
    }

    /**
     * 通过oid获取属性名称
     */
    public static String getOidByProperty(String property) {
        return devicePropertyOidHashMap.get(property);
    }

    /**
     * 从trap队列插入一条数据
     * 
     * @param data 需要入队的数据
     */
    public static void offTrapQueue(String data) {
        queue.offer(data);
    }

    /**
     * 从队列取一条数据
     * 
     * @return String
     */
    public static String pollTrapQueue() {
        return queue.poll();
    }

    public static boolean isEmptyTrapQueue() {
        return queue.isEmpty();
    }

    /**
     * 短信队列增加一条数据
     * 
     * @param data
     * @return
     */
    public static boolean offSmsQueue(SnmpSMSData data) {
        return smsQueue.offer(data);
    }

    /**
     * 从队列取一条数据
     * 
     * @return
     */
    public static SnmpSMSData pollSmsQueue() {
        return smsQueue.poll();
    }

    public static boolean isEmptySmsQueue() {
        return smsQueue.isEmpty();
    }

    /**
     * 从opm队列取一条数据
     *
     * @return DeviceMeasure
     */
    public static DeviceMeasure pollOpmMeasureQueue() {
        return opmMeasureQueue.poll();
    }

    /**
     * opm测量队列增加一条数据
     *
     * @param data DeviceMeasure
     * @return true false
     */
    public static boolean offerOpmMeasureQueue(DeviceMeasure data) {
        return opmMeasureQueue.offer(data);
    }

    public static boolean isOpmMeasureQueueEmpty() {
        return opmMeasureQueue.isEmpty();
    }
}